# req = requests.get('http://localhost:8000/get_json/')
# print(req.json())


# req = requests.get('http://localhost:8000/get_html/')
# print(req.text)


# headers = {
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36 Edg/134.0.0.0'
# }
#
# req = requests.get('http://localhost:8000/chrome_only/', headers= headers)
# print(req.text)


# headers = {
#     'referer': '123.com'
# }
#
# req = requests.get('http://localhost:8000/verify_referer/', headers=headers)
# print(req.text)


# 模拟django登录
import requests
from lxml import html

login_url = 'http://localhost:8000/admin/login/'
session = requests.Session()
response = session.get(login_url)
tree = html.fromstring(response.text)

csrf_token = tree.xpath('//input[@name="csrfmiddlewaretoken"]/@value')[0]
action_url = tree.xpath('//form/@action')[0]
username = 'root'
password = 'root'

login_data = {
    'csrfmiddlewaretoken': csrf_token,
    'username': username,
    'password': password
}

headers = {
    'X-CSRFToken': csrf_token,
    'Content-Type': 'application/x-www-form-urlencoded'
}


login_response = session.post(login_url.rstrip('/') + action_url, data=login_data, headers=headers)

if login_response.status_code == 200:
    print('登录成功！')
else:
    print('登录失败！状态码:', login_response.status_code)
